package com.xuetang9.t9.hotel.services.impl;

import com.xuetang9.t9.hotel.beans.CheckType;
import com.xuetang9.t9.hotel.mapper.ICheckTypeMapper;
import com.xuetang9.t9.hotel.services.IArriveService;
import com.xuetang9.t9.hotel.services.ICheckTypeService;
import com.xuetang9.t9.hotel.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckTypeServiceImpl implements ICheckTypeService {
    @Autowired
    private ICheckTypeMapper checkTypeMapper;

    @Autowired
    private IArriveService arriveService;


    @Override
    public boolean save(CheckType checkType) {
        if (isCheckNull(checkType)) {
            return false;
        }
        return checkTypeMapper.insert(checkType) > 0;
    }

    @Override
    public boolean alter(CheckType checkType) {
        if (isCheckNull(checkType)) {
            return false;
        }
        return checkTypeMapper.update(checkType) > 0;
    }

    @Override
    public boolean remove(Integer id) {
        if(id == null){
            return false;
        }
        arriveService.updateByCheckType(id);
        return checkTypeMapper.delete(id) > 0;
    }

    @Override
    public ResponseResult queryByAll() {
        List<CheckType> checkTypes = checkTypeMapper.selectByAll();
        if(checkTypes == null || checkTypes.size() == 0){
            return ResponseResult.okResult(200,"没有数据");
        }
        return ResponseResult.okResult(checkTypes);
    }

    @Override
    public ResponseResult queryByCount() {
        return null;
    }

    private boolean isCheckNull(CheckType checkType) {
        if(checkType == null){
            return true;
        }
        if(checkType.getChecktypeName() == null || checkType.getChecktypeName().isEmpty()
                || checkType.getChecktypeRate() == null){
            return true;
        }
        return false;
    }
}
